package com.renli.controller;


import com.renli.model.domain.Manager;
import com.renli.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;


@RestController
@RequestMapping("/manager")
@Slf4j
public class ManagerController {
    @Autowired
    private RedisTemplate redisTemplate;
   @PostMapping("/login")
    public R login(@RequestBody @Validated Manager manager, HttpSession session){
       String name = manager.getName();
       String s = DigestUtils.md5DigestAsHex(manager.getPassword().getBytes(StandardCharsets.UTF_8));
     if(name.equals("root")&&s.equals("e10adc3949ba59abbe56e057f20f883e")){
       //  redisTemplate.opsForValue().set("root",name,30, TimeUnit.MINUTES);
         session.setAttribute("root",name);
         return new R(1,"登陆成功");
     }
     return new R(0,"请输入正确的信息");
   }
    @PostMapping("/logout")
    public void logout(HttpSession session){
       session.removeAttribute("root");
    }

}